package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.runchen.gz5055.oms.common.annotation.RequiresPermissions;
import tech.runchen.gz5055.oms.common.base.AbstractController;
import tech.runchen.gz5055.oms.common.enums.UserType;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.modules.system.entity.NoticeEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.CommonMapper;
import tech.runchen.gz5055.oms.modules.system.service.IBarcodeService;
import tech.runchen.gz5055.oms.modules.system.service.ICommonService;
import tech.runchen.gz5055.oms.modules.system.service.IOrderDetailService;

import java.util.List;
import java.util.Map;

/**
 * @author zzj
 */
@Api(tags = "管理平台 - 数据分析")
@RestController
@RequestMapping("/sys/data/analysis")
public class SysDataAnalysisController extends AbstractController {

    @Autowired
    private ICommonService commonService;

    @ApiOperation("各科室订单量及耗材量统计")
    @RequiresPermissions(value = {UserType.NURSE, UserType.SPD})
    @GetMapping("/order")
    public Result order(@RequestParam Map<String, Object> params) {
        StringBuffer sql = new StringBuffer();
        sql.append("select order_tb.delivery_address , COUNT(DISTINCT order_tb.id) orderCount ,SUM(quantity) productCount from order_detail_tb ");
        sql.append("LEFT JOIN order_tb ON order_tb.id = order_detail_tb.order_id ");
        sql.append("GROUP BY order_tb.delivery_address ORDER BY order_tb.delivery_address ");
        List<Map<String, Object>> list = commonService.selectObjectBySql(sql.toString());
        return Result.success(list);
    }

    @ApiOperation("商品消耗量统计")
    @RequiresPermissions(value = {UserType.NURSE, UserType.SPD})
    @GetMapping("/product")
    public Result product(@RequestParam Map<String, Object> params) {
        StringBuffer sql = new StringBuffer();
        sql.append("select CONCAT_WS('',product_tb.product_name,'（',product_tb.model,'）','【',product_tb.factory,'】') product,SUM(quantity) count from order_detail_tb ");
        sql.append("LEFT JOIN order_tb ON order_tb.id = order_detail_tb.order_id ");
        sql.append("LEFT JOIN product_tb ON product_tb.id = order_detail_tb.product_id ");
        sql.append("where product_tb.id IS NOT NULL AND order_tb.status = 3 ");
        sql.append("GROUP BY order_detail_tb.product_id ORDER BY count desc limit 10 ");
        List<Map<String, Object>> list = commonService.selectObjectBySql(sql.toString());
        return Result.success(list);
    }

}
